import 'package:flutter/material.dart';
import 'package:p_location/alarm/obj/job/obj_alarm_job_detail_info.dart';
import 'package:p_location/alarm/utils/trace_judger.dart';
import 'package:p_location/common/widget/tag_view.dart';
import 'package:p_location/common/widget/vertical_info_item.dart';
import 'package:p_location/map/res/search_person_detail.dart';
import 'package:p_location/utils/navigator_util.dart';
import 'package:p_location/utils/net/search_person_util.dart';
import 'package:p_location/utils/string_util.dart';

class PageAlarmJobDetail extends StatefulWidget {
  final AlarmJobDetailInfo alarmJobDetailInfo;

  PageAlarmJobDetail(this.alarmJobDetailInfo);

  @override
  _PageAlarmJobDetailState createState() => _PageAlarmJobDetailState();
}

class _PageAlarmJobDetailState extends State<PageAlarmJobDetail> {
//  String errMsg = "";
//  bool isLoading = true;
//  ObjAlarmVehicle oneKeyDetail;

  SearchPersonDetail fullDetail;

  @override
  void initState() {
    super.initState();
    if(widget.alarmJobDetailInfo != null && StringUtil.isNotEmpty(widget.alarmJobDetailInfo.personSn)){
      _getFullInfoBySn(widget.alarmJobDetailInfo.personSn);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("报警详情"),
          centerTitle: true,
          elevation: 0,
          brightness: Brightness.dark,
          leading: IconButton(
              tooltip: '返回',
              icon: Icon(Icons.arrow_back_ios, color: Colors.white),
              onPressed: () {
                Navigator.of(context).pop();
              }),
        ),
        body: _contentView);
  }

//  get _loadingView => Center(child: CircularProgressIndicator());

  get _contentView => ListView(
        children: [
          VerticalInfoItem("事件", widget.alarmJobDetailInfo.alarmInfo),
          VerticalInfoItem("报警类型", widget.alarmJobDetailInfo.alarmTypeName),
          VerticalInfoItem("报警时间", widget.alarmJobDetailInfo.alarmTime),
          IntrinsicHeight(
            child: Row(
              mainAxisSize: MainAxisSize.max,
              children: [
                Expanded(
                  child: VerticalInfoItem(
                      "报警状态", widget?.alarmJobDetailInfo?.status ?? "-"),
                  flex: 2,
                ),
                Expanded(
                  child: Container(
                      decoration: BoxDecoration(
                          color: Colors.white,
                          border: Border(
                              bottom:
                                  BorderSide(color: Colors.grey, width: 0.4))),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Offstage(
                            offstage: widget.alarmJobDetailInfo.status != '报警中' || TraceJudger.isHide(widget.alarmJobDetailInfo.alarmTypeName),
                            child: TagView(
                              "追踪",
                              paddingLeft: 15,
                              paddingRight: 15,
                              paddingTop: 1,
                              paddingBottom: 1,
                              radius: 5,
                              strokeColor: Color.fromARGB(255, 56, 160, 253),
                              textColor: Color.fromARGB(255, 56, 160, 253),
                              fillColor: Colors.white,
                              onPress: () {
                                if(fullDetail == null){
                                  fullDetail = SearchPersonDetail();
                                  fullDetail.id = widget.alarmJobDetailInfo.id;
                                  fullDetail.name = widget.alarmJobDetailInfo.personName;
                                  fullDetail.sn = widget.alarmJobDetailInfo.personSn;
//                            detail.typeName = _majorHazardObj.typeName;
                                }
                                NavigatorUtil.jumpToLocationPageWithTarget(
                                    context, fullDetail);
                              },
                            ),
                          )
                        ],
                      )),
                ),
              ],
            ),
          ),
          VerticalInfoItem(
              "作业生效时间", getEffectiveTime(widget.alarmJobDetailInfo)),
          VerticalInfoItem("作业许可证号", widget.alarmJobDetailInfo.jobNumber),
          VerticalInfoItem("报警人", widget.alarmJobDetailInfo.personName),
        ],
      );

//  get _exceptionView => NoResultWidget('images/img_no_accident.png', errMsg);

//  _getDetail() async {
//    Map<String, dynamic> params = {};
//    params["id"] = widget.id;
//    await DioHttpClient.requestPost(
//        context,
//        AlarmHttpUrl.ALARM_VEHICLE_URL,
//        params,
//        NetListenerImpl(onSuccess, onError),
//        contentType: NetConstants.POST_TYPE_FORM);
//  }

//  void onSuccess(Response response) {
//    setState(() {
//      if (response != null && response.data != null) {
//        ResAlarmVehicleDetail res =
//            ResAlarmVehicleDetail.fromJson(response.data);
//        if (res != null && res.data != null) {
//          oneKeyDetail = res.data;
//        }
//        isLoading = false;
//      }
//    });
//  }
//
//  void onError(CommonRes errorRes) {
//    setState(() {
//      errMsg = errorRes.msg ?? "请求异常";
//      isLoading = false;
//    });
//  }

  getEffectiveTime(AlarmJobDetailInfo alarmJobDetailInfo) {
    if (alarmJobDetailInfo != null &&
        (StringUtil.isNotEmpty(alarmJobDetailInfo.jobStartTime) ||
            StringUtil.isNotEmpty(alarmJobDetailInfo.jobEndTime))) {
      String left = alarmJobDetailInfo.jobStartTime ?? "-";
      String right = alarmJobDetailInfo.jobEndTime ?? "-";
      return '$left 至 $right';
    }
    return '-';
  }

  _getFullInfoBySn(String keyword) {
    SearchPersonUtil searchPersonUtil =
    SearchPersonUtil(keyword, (List<SearchPersonDetail> _detailList) {
      if (_detailList != null && _detailList.length > 0) {
        SearchPersonDetail searchPersonDetail = _detailList[0];
        if (searchPersonDetail != null) {
          fullDetail = searchPersonDetail;
        }
      }
    }, () {
    });
    searchPersonUtil.requestSearchPerson(context);
  }
}
